package com.punai.customer.controller;

import cn.hutool.core.util.StrUtil;
import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.enums.BusinessType;
import com.punai.customer.domain.CusExpenseCategories;
import com.punai.customer.domain.contants.CusConstants;
import com.punai.customer.domain.vo.CusLinkGkVo;
import com.punai.customer.service.ICusExpenseCategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 材机类别Controller
 * 
 * @author zxw
 * @date 2022-12-04
 */
@RestController
@RequestMapping("/cus/categories")
public class CusExpenseCategoriesController extends BaseController
{
    @Autowired
    private ICusExpenseCategoriesService cusExpenseCategoriesService;

    /**
     * 查询材机类别列表
     */
    @PreAuthorize("@ss.hasPermi('cus:categories:list')")
    @GetMapping("/list")
    public TableDataInfo list(CusExpenseCategories cusExpenseCategories)
    {
        startPage();
        List<CusExpenseCategories> list = cusExpenseCategoriesService.selectCusExpenseCategoriesList(cusExpenseCategories);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('cus:categories:listClGk')")
    @GetMapping("/linkGkList")
    public TableDataInfo listClGk(CusExpenseCategories cusExpenseCategories)
    {
        startPage();
        if (!StrUtil.containsAny(cusExpenseCategories.getType(), CusConstants.CL_TYPE, CusConstants.JX_TYPE)) {
            throw new RuntimeException("参数类型错误");
        }
        cusExpenseCategories.setCompanyId(getCompanyId());
        List<CusExpenseCategories> list = cusExpenseCategoriesService.selectListGk(cusExpenseCategories);
        return getDataTable(list);
    }

    /**
     * 获取材机类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('cus:categories:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(cusExpenseCategoriesService.selectCusExpenseCategoriesById(id));
    }

    /**
     * 新增材机类别
     */
    @PreAuthorize("@ss.hasPermi('cus:categories:add')")
    @Log(title = "材机类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CusExpenseCategories cusExpenseCategories)
    {
        return toAjax(cusExpenseCategoriesService.insertCusExpenseCategories(cusExpenseCategories));
    }

    /**
     * 修改材机类别
     */
    @PreAuthorize("@ss.hasPermi('cus:categories:edit')")
    @Log(title = "材机类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CusExpenseCategories cusExpenseCategories)
    {
        return toAjax(cusExpenseCategoriesService.updateCusExpenseCategories(cusExpenseCategories));
    }

    @Log(title = "修改材机类别关联归口", businessType = BusinessType.UPDATE)
    @PutMapping("/updateCategorieLinkGk")
    public AjaxResult edit(@RequestBody CusLinkGkVo cusLinkGkVo)
    {
        return toAjax(cusExpenseCategoriesService.updateCategorieLinkGk(getCompanyId(),cusLinkGkVo));
    }

    /**
     * 原本为前端叉号所写,后因组件改变,不再使用
     * @param cusLinkGkVo
     * @return
     */

    @Log(title = "删除材机类别关联归口", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteCategorieLinkGk")
    public AjaxResult deleteCategorieLinkGk(@RequestBody CusLinkGkVo cusLinkGkVo)
    {
        return toAjax(cusExpenseCategoriesService.deleteCategorieLinkGk(getCompanyId(),cusLinkGkVo));
    }

    /**
     * 删除材机类别
     */
    @PreAuthorize("@ss.hasPermi('cus:categories:remove')")
    @Log(title = "材机类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(cusExpenseCategoriesService.deleteCusExpenseCategoriesByIds(ids));
    }

    @Log(title = "清空材机类别关联归口", businessType = BusinessType.DELETE)
    @DeleteMapping("/clearLinkGk/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        return toAjax(cusExpenseCategoriesService.clearLinkGk(id,getCompanyId()));
    }

    @GetMapping(value = "/getByNameSearchCJ/{name}/{noInIds}")
    public AjaxResult getByNameSearch(@PathVariable("name") String name,@PathVariable("noInIds") List<String> noInIds)
    {
        if (StrUtil.isEmpty(name.trim())) name = "";
        List<CusExpenseCategories> nameSearch = cusExpenseCategoriesService.getByNameSearchCJ(name,getCompanyId(),noInIds);
        return AjaxResult.success(nameSearch);
    }

    @GetMapping(value = "/getGkItemById/{id}/{gkType}")
    public AjaxResult getGkByCategoryId(@PathVariable("id") String id,@PathVariable("gkType") String gkType)
    {
        return AjaxResult.success(cusExpenseCategoriesService.getGkItemById(id,gkType));
    }
}
